var schoolTreeSetting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    },
    callback: {
        onClick: chooseSchool
    }
};
var schoolTree;

function del(item) {
    var ids = item;
    if(ids == null){
        return ;
    }

    confirm('确定要删除选中的记录？', function() {
        $.ajax({
            type: "GET",
            url: baseURL + "school/school/deleteLowScore?id="+ids,
            success: function (r) {
                if (r.code == 0) {
                    alert('操作成功');
                    $("#jqGrid").trigger("reloadGrid");

                } else {
                    alert(r.msg);
                }

            }
        });
    });
}

function upd(item) {
    var ids = item;
    if(ids == null){
        return ;
    }
    vm.showList = false;
    vm.title = "修改";
    vm.getInfo(ids)
}




function chooseSchool(event, treeId, treeNode) {
    vm.school.id=treeNode.id
    vm.school.name=treeNode.name
    vm.schoolName=treeNode.name
    vm.reload()
}

$(function () {
    vm.getSchoolTree()
    $("#jqGrid").jqGrid({
        url: baseURL + 'school/school/getSchoolLowestScore?id='+vm.school.id,
        datatype: "json",
        colModel: [
            { label: '学校id', name: 'schoolId', index: 'school_id', width: 80, hidden:true },
            { label: '年份', name: 'year', index: 'year', width: 80 },
            { label: '学校代码', name: 'schoolCode', index: 'school_code', width: 80 },
            { label: '学校名称', name: 'schoolName', index: 'school_name', width: 80 },
            { label: '普通文科专业最低分', name: 'putongwenke', index: 'putongwenke', width: 80 },
            { label: '普通理科专业最低分', name: 'putonglike', index: 'putonglike', width: 80 },
            { label: '校企合作文科专业最低分', name: 'xiaoqihezuowenke', index: 'xiaoqihezuowenke', width: 80 },
            { label: '校企合作理科专业最低分', name: 'xiaoqihezuolike', index: 'xiaoqihezuolike', width: 80 },
            { label: '3+2连读文科专业最低分', name: 'lianduwenke', index: 'lianduwenke', width: 80 },
            { label: '3+2连读理科专业最低分', name: 'liandulike', index: 'liandulike', width: 80 },
            { label: '中外合作办学文科最低分', name: 'zhongwaihezuowenke', index: 'zhongwaihezuowenke', width: 80 },
            { label: '中外合作办学理科最低分', name: 'zhongwaihezuolike', index: 'zhongwaihezuolike', width: 80 },
            { label: '艺术文科最低分', name: 'yishuwenke', index: 'yishuwenke', width: 80 },
            { label: '艺术理科最低分', name: 'yishulike', index: 'yishulike', width: 80 },
            { label: '春考文科最低分', name: 'chunkaowenke', index: 'chunkaowenke', width: 80 },
            { label: '春考理科最低分', name: 'chunkaolike', index: 'chunkaolike', width: 80 },
            { label: '专科普通文科最低分', name: 'zhuankeputongwenke', index: 'zhuankeputongwenke', width: 80 },
            { label: '专科普通理科最低分', name: 'zhuankeputonglike', index: 'zhuankeputonglike', width: 80 },
            { label: '专科校企文科最低分', name: 'zhuankexiaoqiwenke', index: 'zhuankexiaoqiwenke', width: 80 },
            { label: '专科校企理科最低分', name: 'zhuankexiaoqilike', index: 'zhuankexiaoqilike', width: 80 },
            { label: '专科中外合作文科最低分', name: 'zhuankezhongwaihezuowenke', index: 'zhuankezhongwaihezuowenke', width: 80 },
            { label: '专科中外合作理科最低分', name: 'zhuankezhongwaihezuolike', index: 'zhuankezhongwaihezuolike', width: 80 },
            { label: '操作', name: 'id', width: 80 ,
                formatter: function (data) {
                    return '<span class="label label-warning" onclick="upd('+data+')">修改</span>&nbsp;<span class="label label-danger" onclick="del('+data+')">删除</span>';

                }}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        // pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });

})


var vm = new Vue({
    el:'#rrapp',
    data:{
        school:{id:0,name:null},
        schoolName:null,
        showList:true,
        title:null,
        noHasSchoolYear:["2019","2018","2017","2016"],
        lowscore:{}
    },
    methods: {
        courseTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择菜单",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#menuLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = zCoursetree.getSelectedNodes();
                    if(node[0].id<10000) {
                        //选择上级菜单
                        vm.teachPlan.courseId=node[0].id;
                        vm.teachPlan.title = node[0].name;
                        layer.close(index);
                    }
                }
            });
        },
        getSchoolTree: function(menuId){
            //加载学校树
            $.get(baseURL + "school/school/getSchoolTree", function(r){
                schoolTree = $.fn.zTree.init($("#schoolTree"), schoolTreeSetting, r.schoolList);
                var node = schoolTree.getNodeByParam("id", 0);
                schoolTree.selectNode(node);
            })



        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                url: baseURL + 'school/school/getSchoolLowestScore?id='+vm.school.id,
                page:page
            }).trigger("reloadGrid");
        },
        add: function(){
            if(vm.school.id>10000){
                alert("请先选择学校")
            }else {
                vm.showList = false;
                vm.title = "新增";
                vm.lowscore = {year: null,schoolId:vm.school.id}
            }
        },
        saveOrUpdate:function () {
            console.log(vm.lowscore)
            if(vm.lowscore.year==null){
                alert("年份有误")

            }else{
                var url = vm.lowscore.id == null ? "school/school/saveLowScore" : "school/school/updateLowScore";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.lowscore),
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(index){
                                vm.reload();
                                //   location. reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            }
        },
        getInfo:function (id) {
            $.get(baseURL + "school/school/schoolYear/"+id, function(r){
                vm.lowscore = r.lowscore;
            });
        }

    }

});